package com.lianzhihui.romeex.utils;

import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.inter.SetListener;

/**
 * Description:
 * Author: RAMON
 * CreateDate: 2020/11/4 11:12 AM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class UiUtils {

    /**
     * 输入框是否可见
     *
     * @param isCanSee 可见状态，true 可见
     * @param et       输入框
     */
    public static void setHiden(boolean isCanSee, EditText et) {
        if (isCanSee) {
            //选择状态 显示明文--设置为可见的密码
            //mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            //默认状态显示密码--设置文本 要一起写才能起作用  InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
            //mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            et.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        et.setSelection(et.length());
    }

    /**
     * 设置眼睛和输入框 可见
     *
     * @param eyeIv
     * @param inputEt
     */
    public static void setTextEyes(ImageView eyeIv, EditText inputEt) {
        if (eyeIv.isSelected()) {
            eyeIv.setSelected(false);
            eyeIv.setImageResource(R.drawable.icon_hide);
            setHiden(false, inputEt);
        } else {
            eyeIv.setSelected(true);
            eyeIv.setImageResource(R.drawable.icon_public);
            setHiden(true, inputEt);
        }
    }

    /**
     * 设置 输入框 规则,只能小数点后两位
     * @param editText
     */
    public static void setEditText(EditText editText){
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        editText.addTextChangedListener(textWatcher);

    }

    /**
     * 设置 输入框 规则,只能小数点后两位
     * @param editText
     */
    public static void setEditText8(EditText editText){

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 8) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 9);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        editText.addTextChangedListener(textWatcher);

    }
    /**
     * 设置 输入框 规则,只能小数点后两位
     * @param editText
     */
    public static void setEditText8(EditText editText,SetListener listener){

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 8) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 9);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
                try {
                    double num = Double.parseDouble(editText.getText().toString());
                    listener.getInput(num);
                }catch(Exception e){
                    listener.getInput(0);
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        editText.addTextChangedListener(textWatcher);

    }

}
